package com.cloud.lan.api.manage;

import com.cloud.lan.api.dto.resp.LanBookingActivityRecordExport;
import com.cloud.lan.entity.model.dataobject.LanBookingActivityRecordDO;
import com.cloud.lan.entity.utils.ExcelUtil;
import com.cloud.lan.service.ILanBookingActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;

/**
 * Created by zhouquan on 2022/5/21.
 */
@RestController
@RequestMapping(value = {"/v1/manage/booking/activity/"}, method = {RequestMethod.GET, RequestMethod.POST}, produces = {"application/json"})
public class LanBookingActivityManageApi {

    private ExcelUtil excelUtil = ExcelUtil.getExcelUtil();

    @Autowired
    private ILanBookingActivityService iLanBookingActivityService;

    @RequestMapping("export")
    @ResponseBody
    public ResponseEntity<byte[]> export(Long activityId) throws Exception{
        List<LanBookingActivityRecordExport> exportList = buildExportList(activityId);
        byte[] fileContext = null;
        if(ObjectUtil.isNotEmpty(exportList)){
            String[] title = {"用户活动记录ID","预约活动ID","用户ID","姓名","性别","平台CODE","平台名","平台用户账号","产品ID","产品名","寄送省","寄送市","寄送地址1","寄送地址2","评价标题","用户评价"};
            fileContext = excelUtil.createExcelFromList(title,exportList);
        }
        String fileName = "用户参加活动"+activityId+"的记录" +".xlsx";
        fileName= URLEncoder.encode(fileName, "UTF-8");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", fileName);
        return new ResponseEntity<>(fileContext, headers, HttpStatus.CREATED);
    }

    private List<LanBookingActivityRecordExport> buildExportList(Long activityId){
        List<LanBookingActivityRecordDO>  recordDOList = iLanBookingActivityService.queryAllUserACR(activityId);
        if(ObjectUtil.isNotEmpty(recordDOList)){
            List<LanBookingActivityRecordExport>  exportList = new ArrayList<>();
            recordDOList.stream().forEach(rd->{
                LanBookingActivityRecordExport export = new LanBookingActivityRecordExport();
                export.setId(rd.getId());
                export.setActivityId(rd.getActivityId());
                export.setUserId(rd.getUserId());
                export.setUserName(rd.getLastName()+rd.getFirstName());
                export.setUserSex(rd.getUserSex() == 1?"男":"女");
                export.setPlatformCode(rd.getPlatformCode());
                export.setPlatformName(rd.getPlatformName());
                export.setPlatformUserName(rd.getPlatformUserName());
                export.setProductId(rd.getProductId());
                export.setProductName(rd.getProductName());
                export.setSendProvince(rd.getSendProvince());
                export.setSendCity(rd.getSendCity());
                export.setSendAddress1(rd.getSendAddress1());
                export.setSendAddress2(rd.getSendAddress2());
                exportList.add(export);
            });
            return exportList;
        }
        return null;
    }
}
